

function MakeCmdIndex(cmdBuf: egret.ByteArray): number {
    return <number>(cmdBuf.bytes[0] * 255 + cmdBuf.bytes[1])
}

type MessageHandle<T> = (cmdBuf: T) => void;
interface IMessageHandleMgr {
    InitAll()
    Register<T>(type: ProtocolType<T>, handler: MessageHandle<T>)
    Process(cmdBuf: egret.ByteArray)
}

class MessageHandleMgrBase implements IMessageHandleMgr {
    private _allHandle = {}
    private _allType = {}

    public InitAll() {
    }
    public Register<T>(type: ProtocolType<T>, handler: MessageHandle<T>) {
        this._allHandle[type.Index] = handler
        this._allType[type.Index] = type
    }
    public Process(cmdBuf: egret.ByteArray) {
        let index = MakeCmdIndex(cmdBuf)
        let handler = this._allHandle[index]
        let type = this._allType[index]
        if (handler) {
            handler(type.UnpackMessage(cmdBuf))
        } else {
            console.log("收到未处理数据：" + index)
        }
    }
}
